<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理" name="first">
            <el-row style="height: 60px">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <el-table border style="width: 100%" :data="RoleList">
              <el-table-column type="index" align="center" label="序号" width="120" />
              <el-table-column prop="name" align="center" label="角色" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column label="操作" align="center" width="280">
                <!-- 这三个操作的接口函数需要参数, 要想到作用域插槽 slot-scope -->
                <template slot-scope="{row}">
                  <el-button type="success" size="small">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(row.id)">编辑</el-button>
                  <el-button type="danger" size="small" @click="deleteRow(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                :current-page="page.page"
                :page-sizes="[1, 4, 5, 7]"
                :page-size="page.pagesize"
                layout="total, sizes, prev, pager, next"
                :total="page.total"
                @current-change="changePage"
                @size-change="changeSize"
              /></el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="企业地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="companyInfo.companyPhone" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title="roleForm.id?'编辑角色':'新增角色'"
          :visible="showDialog"
          @close="btnCancel"
        >
          <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
            <el-form-item prop="name" label="角色名称"><el-input v-model="roleForm.name" /></el-form-item>
            <el-form-item label="角色描述"><el-input v-model="roleForm.description" /></el-form-item>
          </el-form>
          <el-row type="flex" justify="center">
            <el-col>
              <el-button size="small" @click="btnCancel">取消</el-button>
              <el-button type="primary" size="small" @click="btnOK">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>
      </el-card>
    </div>

  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'HrSassIndex',

  data() {
    return {
      // 放置页码及相关数据
      page: {
        page: 1, // 页码
        pagesize: 4, // 每页条数
        total: 0 // 记录总数
      },
      RoleList: [], // 角色信息列表
      companyInfo: {}, // 公司信息
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }

    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.loadRoleList()
    this.LoadCompanyInfo()
  },
  mounted() {

  },

  methods: {
    async  loadRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.RoleList = rows
      this.page.total = total
    },
    changePage(newPage) {
      // newPage当前点击的页码
      this.page.page = newPage// 将当前页码赋值给当前最新的页码
      // 重新渲染页面
      this.loadRoleList()
    },
    changeSize(newSize) {
      this.page.pagesize = newSize
      this.loadRoleList()
    },
    async  LoadCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.companyId)
    },
    async deleteRow(id) {
      try {
        await this.$confirm('确认删除角色吗?')
        // 只有点击确定才会执行删除操作操作  因为加了await
        await deleteRole(id)// 调用删除接口
        this.loadRoleList()// 重新渲染
        this.$message.success('删除角色成功')
      } catch (error) {
        this.$message.error('删除失败')
      }
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)// 实现数据的回写
      this.showDialog = true
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()// 若不传入回调函数，则会返回一个 promise 可以用async await
        // 只有较验成功,才会执行await的下方内容
        // roleForm这个对象有id就是编辑,没有id就是新增
        // 调用更新接口
        if (this.roleForm.id) {
          // 编辑角色
          await updateRole(this.roleForm)
        } else {
          // 新增角色
          await addRole(this.roleForm)
        }
        this.loadRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.showDialog = false
      // 移除较验
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
<style></style>
